package space.misiro.ledgers.token.exchange

import java.lang.System.getenv

class ConfigurationTokenResourceConfiguration(
    val longLivedTokenRole: String
) {

    companion object {
        const val KEYCLOAK_LONG_LIVED_ROLE_NAME = "KEYCLOAK_LONG_LIVED_ROLE_NAME"
        const val DEFAULT_KEYCLOAK_LONG_LIVED_ROLE_NAME = "long_lived_token"

        fun readFromEnvironment() = ConfigurationTokenResourceConfiguration(readLongLivedRoleFromEnvironment())

        fun readLongLivedRoleFromEnvironment() =
            getenv(KEYCLOAK_LONG_LIVED_ROLE_NAME).takeUnless { it == null || it.trim().isEmpty() }
                ?: DEFAULT_KEYCLOAK_LONG_LIVED_ROLE_NAME
    }
}